package com.reggie.background.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.reggie.background.common.R;
import com.reggie.background.entity.Employee;
import com.reggie.background.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /**
     * 用户登录
     * @param request
     * @param employee
     * @return
     */
    @PostMapping ("/login")
    public R<Employee> login(HttpServletRequest request,@RequestBody Employee employee) {
        String password = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        LambdaQueryWrapper<Employee> eqw = new LambdaQueryWrapper<>();
        eqw.eq(Employee::getUsername,employee.getUsername());
        Employee one = employeeService.getOne(eqw);
        if (one ==null){
            return R.error("该用户不存在！");
        }
        if (!one.getPassword().equals(password)){
            return R.error("用户名与密码不匹配！");
        }
        if (one.getStatus()==0){
            return R.error("该用户已经被冻结！");
        }
        request.getSession().setAttribute("employee",one.getId());
        return R.success(one);

    }

    /**
     * 用户退出登录
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public  R<String> logout(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        return R.success("已退出登录！");
    }

    /**
     * 新增员工
     * @param request
     * @param employee
     * @return
     */
    @PostMapping
    public R<String> insertEmployee(HttpServletRequest request,@RequestBody Employee employee){
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //时间以及用户更新已经自动填充
        employeeService.save(employee);



        return R.success("成功添加员工！");
    }

    /**
     * 员工分页查询列表
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page<Employee>> getpage(int page, int pageSize, String name){
        Page<Employee> pageList = new Page<Employee>(page,pageSize);
        LambdaQueryWrapper<Employee> qw=new LambdaQueryWrapper<Employee>();
        qw.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        qw.orderByDesc(Employee::getUpdateTime);
        employeeService.page(pageList,qw);
        return  R.success(pageList);
    }

    /**
     * 根据id查询员工
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> getOne(@PathVariable String id){
        LambdaQueryWrapper<Employee> qw = new LambdaQueryWrapper<Employee>();
        qw.eq(Employee::getId,id);
        Employee one = employeeService.getOne(qw);

        return R.success(one);
    }

    /**
     * 更新员工信息
     * @param request
     * @param employee
     * @return
     */
    @PutMapping
    public  R<String> update(HttpServletRequest request,@RequestBody Employee employee){
        if (employee==null){
            return R.error("更新员工失败！");
        }
        log.info("本次请求线程id为{}",Thread.currentThread().getId());
        boolean b = employeeService.updateById(employee);
        if (!b){
            return R.error("更新员工失败！");
        }
        return R.success("更新员工成功");
    }
}
